package de.wefunkdroid.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import android.content.Context;
import android.util.Log;

public class DataSave {

	File appfolder;
	Context appcontext;
	static boolean hassavedState = false;

	public DataSave(Context ctx) {
		appcontext = ctx;

	}

	public Object[] getObject() {
		Object[] objects = null;
		FileInputStream restore = null;
		try {

			restore = appcontext.openFileInput("save");
			ObjectInputStream objin = new ObjectInputStream(restore);
			objects = (Object[]) objin.readObject();
			System.err.println(objects.length);
			objin.close();
			// restore.close();
			return objects;

		} catch (Exception e) {
			e.printStackTrace();
			Log.d("DataSave", "Error", e.getCause());
		}
		return null;
	}

	public void writeObject(Object obj) {
		Object[] objects = null;
		FileInputStream restore = null;
		FileOutputStream save = null;
		if (hassavedState) {
			String[] flist = appcontext.fileList();
			for (int i = 0; i < flist.length; i++) {
				if (flist[i].equals("save")) {
					hassavedState = true;
				}
			}
		}
		try {
			if (hassavedState) {

				restore = appcontext.openFileInput("save");
				ObjectInputStream objin = new ObjectInputStream(restore);
				objects = (Object[]) objin.readObject();
				System.err.println(objects.length);
				objin.close();
				restore.close();
			}

			if (objects != null) {

				Object[] tmp = new Object[objects.length + 1];
				if (tmp.length == 1)
					tmp[0] = obj;
				else
					for (int i = 0; i < objects.length; i++) {
						tmp[i] = objects[i];
					}
				tmp[tmp.length - 1] = obj;
				save = appcontext.openFileOutput("save", Context.MODE_PRIVATE);
				ObjectOutputStream ostr = new ObjectOutputStream(save);
				ostr.writeObject(tmp);
				ostr.flush();
				ostr.close();
				save.close();
				tmp = null;
				hassavedState = true;
				Log.d(DataSave.class.getSimpleName(), "Daten geschrieben");

			} else {
				Object[] tmp = new Object[1];

				tmp[0] = obj;

				tmp[tmp.length - 1] = obj;
				save = appcontext.openFileOutput("save", Context.MODE_PRIVATE);
				ObjectOutputStream ostr = new ObjectOutputStream(save);
				ostr.writeObject(tmp);
				ostr.flush();
				ostr.close();
				save.close();
				tmp = null;
				hassavedState = true;
				Log.d(DataSave.class.getSimpleName(), "Daten geschrieben");
			}
		} catch (Exception e) {
			Log.e("DataSave", "Error", e.getCause());
		}
		objects = null;

	}

	public void deleteAllData() {
		if (appcontext.deleteFile("save"))
			Log.d("DataSave", "Daten geloescht");
	}

	public void deleteData(Object dat) {
		try {
			Object[] objects = null;
			ArrayList<Object> objlist;
			FileInputStream restore = null;
			if (hassavedState) {

				restore = appcontext.openFileInput("save");
				ObjectInputStream objin = new ObjectInputStream(restore);
				objects = (Object[]) objin.readObject();
				System.err.println(objects.length);
				objin.close();
				restore.close();
			}
			int point = -1;
			objlist = new ArrayList<Object>();
			for (int i = 0; i < objects.length; i++) {
				if (objects[i].equals(dat)) {
					point = i;
				}
				objlist.add(objects[i]);
			}
			if (point != -1)
				if (objlist.remove(point) != null)
					Log.d(this.getClass().getSimpleName(), "Daten entfernt");

		} catch (Exception e) {
			Log.e(this.getClass().getSimpleName(), "Fehler beim Loeschen", e);
		}
	}

}
